<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="$store.state.imgUrl" @click="togglePhoneFn"/>
           <input
               type="file"
               ref="iptFile"
               v-show="false"
               accept="image/*"
               @change="changeFn"
               />
        </template>
      </van-cell>
      <van-cell title="性别" is-link  :value="sexObj[user.gender]"/>
      <van-cell title="姓名" is-link  :value="$store.state.userName" @click="editNameFn"/>
       <van-dialog v-model="show" title="姓名" show-cancel-button :beforeClose="beforeCloseFn">
        <input type="text" v-focus v-model="userName">
</van-dialog>
      <van-cell title="生日" is-link  :value="user.birthday" @click="editBirthdayFn"/>
<van-popup v-model="isShow" position="bottom" :style="{ height: '30%' }" >
  <van-datetime-picker
  v-model="currentDate"
  type="date"
  title="选择年月日"
  :min-date="minDate"
  :max-date="maxDate"
  @confirm="confirmFn"
/>
</van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import { editUserAPI, editPhoneAPI, editMessageAPI } from '@/api'
import dayjs from 'dayjs'
import { Notify } from 'vant'
import { mapMutations } from 'vuex'
export default {
  data () {
    return {
      user: {},
      sexObj: {
        0: '男',
        1: '女',
        '-1': '保密'
      },
      show: false,
      isShow: false,
      userName: '',
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date()
    }
  },
  name: 'UserEdit',
  async created () {
    const res = await editUserAPI()
    this.user = res.data.data
    this.setUserNameMutations(this.user.name)
    this.setImgUrl(this.user.photo)
    // console.log(this.user)
  },
  methods: {
    ...mapMutations(['setImgUrl', 'setUserNameMutations']),
    async confirmFn () {
      const dateStr = dayjs(this.currentDate).format(('YYYY-MM-DD'))
      console.log(dateStr)
      const obj = { birthday: dateStr }
      const res = await editMessageAPI(obj)
      console.log(res)
      this.user.birthday = dateStr
      this.isShow = false
    },
    async beforeCloseFn (action, done) {
      if (action === 'cancel') {
        done()
      } else {
        if (/^[A-Za-z\u4e00-\u9fa5]{1,7}$/.test(this.userName)) {
          const obj = { name: this.userName }
          const res = await editMessageAPI(obj)
          console.log(res)
          // this.user.name = this.userName
          this.setUserNameMutations(this.userName)
          done()
        } else {
          Notify({
            type: 'danger',
            message: '姓名必须在1-7的字符串'
          })
          // 阻止弹窗关闭
          done(false)
        }
      }
    },
    editNameFn () {
      this.show = true
      this.userName = this.user.name
    },
    editBirthdayFn () {
      this.isShow = true
      this.currentDate = new Date(this.user.birthday)
    },
    togglePhoneFn () {
      this.$refs.iptFile.click()
    },
    async changeFn (e) {
      console.log(e.target.files)
      if (e.target.files.length > 0) {
        const fd = new FormData()
        fd.append('photo', e.target.files[0])
        const res = await editPhoneAPI(fd)
        console.log(res)
        this.user.photo = res.data.data.photo
        this.setImgUrl(res.data.data.photo)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/ .van-dialog__content{
  text-align: center;
}
/deep/ .van-dialog__content input{
  text-align: center;
  border: 0;
}
</style>
